package com.eralink.unionuhfreader.netty;

import android.util.Log;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    private static final String TAG = "NettyClientHandler";
    private NettyListener listener;

    public NettyClientHandler(NettyListener listener) {
        this.listener = listener;
    }

    //每次给服务器发送的东西， 让服务器知道我们在连接中哎
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.WRITER_IDLE) {
                ctx.channel().writeAndFlush("Heartbeat" + System.getProperty("line.separator"));
            }
        }
    }

    /**
     * 连接成功
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Log.e(TAG, "channelActive 连接成功");
        super.channelActive(ctx);
       // ElectronicScale.alreadyConnect.put(ctx.channel().id().toString(),ctx);
        listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_SUCCESS);
        ctx.writeAndFlush("UPL\tINF\t\r\n");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        Log.e(TAG, "channelInactive 连接断开");
    //    ElectronicScale.alreadyConnect.remove(ctx.channel().id().toString());
        listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_ERROR);
    }

    //接收消息的地方， 接口调用返回到activity了
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String msgStr=msg.toString();
        Log.d(TAG, ">>>>接收到消息"+msg.toString());
        listener.onMessageResponse(msg);
        if (msgStr.contains("DWL")){
            String macinfo=msgStr.substring(msgStr.indexOf("INM\t")+4,msgStr.length());
            String macAddress=macinfo.substring(0,macinfo.indexOf("\t"));
            String macname=macinfo.substring(macinfo.indexOf("INF\t")+4,macinfo.length());
            String name=macname.substring(0,macname.indexOf("\t"));
            Log.d(">>>收到消息",name+""+macAddress+" "+ctx.channel().id());
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // 当引发异常时关闭连接。
        Log.e(TAG, "exceptionCaught");
       // ElectronicScale.alreadyConnect.remove(ctx.channel().id().toString());
        listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_ERROR);
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
        Log.d("channelUnregistered",ctx.channel().id().toString());
        //ElectronicScale.alreadyConnect.remove(ctx.channel().id().toString());
        listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_ERROR);
    }
}
